﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace TARS.AdminCP
{
    public partial class Prices : System.Web.UI.Page
    {
        TARSDelivery_EprojectEntities db = new TARSDelivery_EprojectEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Page.IsPostBack))
            {
                //Load data to combobox Branches Zone
                var getBranchesZone = db.t_BrachesZone.Where(x => x.Active == true);

                RadComboBoxItem comboboxitem = new RadComboBoxItem();
                comboboxitem.Text = "Select Branches";
                comboboxitem.Value = "All";
                cboBranchesInfo.Items.Add(comboboxitem);

                foreach (var get in getBranchesZone)
                {
                    comboboxitem = new RadComboBoxItem();
                    comboboxitem.Text = get.ZoneName;
                    comboboxitem.Value = get.ID.ToString();
                    cboBranchesInfo.Items.Add(comboboxitem);
                }
                var getPrices = db.v_Prices.Where(x => x.BranchesZoneID == null && x.CarrierTypeID == null).OrderByDescending(x => x.Prices).ThenByDescending(x => x.CreatedTime);
                RadGrid1.DataSource = getPrices.ToList();
                RadGrid1.DataBind();
            }
            else
            {
                if (btnShow.Enabled == true && cboCustom.SelectedItem.Value != "All")
                {
                    if (btnDropZone.Checked == true)
                    {
                        Guid DropZoneID = new Guid(cboCustom.SelectedItem.Value);
                        var getPrices = db.v_Prices.Where(x => x.DropZoneID == DropZoneID).OrderByDescending(x => x.WeightTypeName).ThenByDescending(x => x.CreatedTime);
                        RadGrid1.DataSource = getPrices.ToList();
                    }
                    else
                    {
                        Guid WeightTypeID = new Guid(cboCustom.SelectedItem.Value);
                        var getPrices = db.v_Prices.Where(x => x.WeightTypeID == WeightTypeID).OrderByDescending(x => x.DropZoneName).ThenByDescending(x => x.CreatedTime);
                        RadGrid1.DataSource = getPrices.ToList();
                    }
                }
            }
        }

        protected void cboBranchesInfo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadGrid1.Visible = false;
            cboCarrierType.Items.Clear();
            cboCarrierType.Text = "";
            btnDropZone.Checked = false;
            btnWeightType.Checked = false;
            btnWeightType.Enabled = false;
            btnDropZone.Enabled = false;
            cboCustom.Enabled = false;
            cboCustom.Text = "";
            cboCustom.Items.Clear();
            btnShow.Enabled = false;

            if (e.Value != "All")
            {
                cboCarrierType.Enabled = true;

                Guid brancheszoneid = new Guid(e.Value);
                var getCarrierType = db.t_CarrierType.Where(x => x.Active == true && x.BranchesZoneID == brancheszoneid);

                RadComboBoxItem comboboxitem = new RadComboBoxItem();
                comboboxitem.Text = "Select Carrier Type";
                comboboxitem.Value = "All";
                cboCarrierType.Items.Add(comboboxitem);

                foreach (var get in getCarrierType)
                {
                    comboboxitem = new RadComboBoxItem();
                    comboboxitem.Text = get.CarrierTypeName;
                    comboboxitem.Value = get.ID.ToString();
                    cboCarrierType.Items.Add(comboboxitem);
                }
            }
        }

        protected void cboCarrierType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadGrid1.Visible = false;
            cboCustom.Items.Clear();
            cboCarrierType.Text = "";
            btnShow.Enabled = false;
            btnDropZone.Checked = false;
            btnWeightType.Checked = false;
            cboCustom.Enabled = false;
            cboCustom.Text = "";

            if (e.Value != "All")
            {
                btnWeightType.Enabled = true;
                btnDropZone.Enabled = true;
            }
            else
            {
                btnWeightType.Enabled = false;
                btnDropZone.Enabled = false;
            }
        }



        protected void cboDropZone_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void cboWeightType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {

        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }

        protected void RadGrid1_ItemDataBound1(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                Guid CarrierTypeID = new Guid(cboCarrierType.SelectedItem.Value);

                GridEditableItem editItem = (GridEditableItem)e.Item;
                RadComboBox cboDropZone = (RadComboBox)editItem.FindControl("cboDropZone");
                RadComboBox cboWeightType = (RadComboBox)editItem.FindControl("cboWeightType");
                var getDropZone = db.t_DropZone.Where(x => x.CarrierTypeID == CarrierTypeID && x.Active == true).OrderByDescending(x => x.DropZoneName);
                cboDropZone.DataSource = getDropZone.ToList();
                cboDropZone.DataTextField = "DropZoneName";
                cboDropZone.DataValueField = "ID";
                cboDropZone.DataBind();

                var getWeightType = db.t_WeightType.Where(x => x.CarrierTypeID == CarrierTypeID && x.Active == true).OrderByDescending(x => x.WeightTypeName);
                cboWeightType.DataSource = getWeightType.ToList();
                cboWeightType.DataTextField = "WeightTypeName";
                cboWeightType.DataValueField = "ID";
                cboWeightType.DataBind();


                //Set Visible in Insert & Update Item
                GridEditFormItem edit = (GridEditFormItem)e.Item;
                edit["NewID"].Visible = false;
                edit["CreatedTime"].Visible = false;

                //Set style in Update Item
                if (!(e.Item is IGridInsertItem))
                {

                    int AutoCount = 0;
                    int GetIndex = 0;
                    string getDropZoneName = ((v_Prices)e.Item.DataItem).DropZoneName;
                    foreach (RadComboBoxItem getitem in cboDropZone.Items)
                    {
                        if (getitem.Text == getDropZoneName)
                        {
                            GetIndex = AutoCount;
                        }
                        AutoCount++;
                    }
                    cboDropZone.SelectedIndex = GetIndex;

                    AutoCount = 0;
                    GetIndex = 0;
                    string getWeightTypeName = ((v_Prices)e.Item.DataItem).WeightTypeName;
                    foreach (RadComboBoxItem getitem in cboWeightType.Items)
                    {
                        if (getitem.Text == getWeightTypeName)
                        {
                            GetIndex = AutoCount;
                        }
                        AutoCount++;
                    }
                    cboWeightType.SelectedIndex = GetIndex;
                }
                else
                {
                    if (btnDropZone.Checked == true)
                    {
                        cboWeightType.Enabled = true;
                        cboDropZone.Enabled = false;
                        cboDropZone.Text = cboCustom.SelectedItem.Text;
                        Session["DropZoneID"] = cboCustom.SelectedItem.Value;
                        if (Session["WeightTypeID"] != null)
                            Session["WeightTypeID"] = null;
                    }
                    else
                    {
                        cboDropZone.Enabled = true;
                        cboWeightType.Enabled = false;
                        cboWeightType.Text = cboCustom.SelectedItem.Text;
                        Session["WeightTypeID"] = cboCustom.SelectedItem.Value;
                        if (Session["DropZoneID"] != null)
                            Session["DropZoneID"] = null;
                    }
                }
            }

        }

        private void ResetGrid()
        {
            RadGrid1.Visible = false;
            RadGrid1.MasterTableView.ClearSelectedItems();
            RadGrid1.MasterTableView.ClearEditItems();
            RadGrid1.MasterTableView.ClearChildEditItems();
            RadGrid1.MasterTableView.IsItemInserted = false;
        }

        protected void btnDropZone_Click(object sender, EventArgs e)
        {
            ResetGrid();
            btnShow.Enabled = false;
            cboCustom.Items.Clear();
            cboCustom.Text = "";
            cboCustom.Enabled = true;

            Guid CarrierTypeID = new Guid(cboCarrierType.SelectedItem.Value);
            var getDropZone = db.t_DropZone.Where(x => x.Active == true && x.CarrierTypeID == CarrierTypeID);

            RadComboBoxItem comboboxitem = new RadComboBoxItem();
            comboboxitem.Text = "Select Drop Zone";
            comboboxitem.Value = "All";
            cboCustom.Items.Add(comboboxitem);

            foreach (var get in getDropZone)
            {
                comboboxitem = new RadComboBoxItem();
                comboboxitem.Text = get.DropZoneName;
                comboboxitem.Value = get.ID.ToString();
                cboCustom.Items.Add(comboboxitem);
            }
        }

        protected void btnWeightType_Click(object sender, EventArgs e)
        {
            ResetGrid();
            btnShow.Enabled = false;
            cboCustom.Items.Clear();
            cboCustom.Text = "";
            cboCustom.Enabled = true;

            Guid CarrierTypeID = new Guid(cboCarrierType.SelectedItem.Value);
            var getWeightType = db.t_WeightType.Where(x => x.Active == true && x.CarrierTypeID == CarrierTypeID);

            RadComboBoxItem comboboxitem = new RadComboBoxItem();
            comboboxitem.Text = "Select Weight Type";
            comboboxitem.Value = "All";
            cboCustom.Items.Add(comboboxitem);

            foreach (var get in getWeightType)
            {
                comboboxitem = new RadComboBoxItem();
                comboboxitem.Text = get.WeightTypeName;
                comboboxitem.Value = get.ID.ToString();
                cboCustom.Items.Add(comboboxitem);
            }
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            RadGrid1.Visible = true;
            LoadGrid();
        }

        private void LoadGrid()
        {
            if (btnShow.Enabled == true)
            {
                if (btnDropZone.Checked == true)
                {
                    Guid DropZoneID = new Guid(cboCustom.SelectedItem.Value);
                    var getPrices = db.v_Prices.Where(x => x.DropZoneID == DropZoneID).OrderByDescending(x => x.DropZoneName).ThenByDescending(x => x.WeightTypeName).ThenByDescending(x => x.CreatedTime);
                    RadGrid1.DataSource = getPrices.ToList();
                    RadGrid1.DataBind();
                }
                else
                {
                    Guid WeightTypeID = new Guid(cboCustom.SelectedItem.Value);
                    var getPrices = db.v_Prices.Where(x => x.WeightTypeID == WeightTypeID).OrderByDescending(x => x.DropZoneName).ThenByDescending(x => x.WeightTypeName).ThenByDescending(x => x.CreatedTime);
                    RadGrid1.DataSource = getPrices.ToList();
                    RadGrid1.DataBind();
                }
            }
        }

        protected void cboCustom_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            ResetGrid();
            if (e.Value != "All")
                btnShow.Enabled = true;
            else
                btnShow.Enabled = false;
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.EditCommandName)
            {
                RadGrid1.MasterTableView.IsItemInserted = false; //Close Insert Item if Update Item Open
            }
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                RadGrid1.MasterTableView.ClearEditItems(); //Close Update Item if Insert Item Open
                Guid newguid = Guid.NewGuid();
                Hashtable defaultvalues = new Hashtable();
                defaultvalues["NewID"] = newguid.ToString();
                defaultvalues["CreatedTime"] = DateTime.Now;
                defaultvalues["Active"] = true;
                e.Item.OwnerTableView.InsertItem(defaultvalues);
            }
        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            //Insert New Item
            GridEditableItem item = e.Item as GridEditableItem;
            String Prices = (item["Prices"].Controls[0] as TextBox).Text;
            Boolean Active = (item["Active"].Controls[0] as CheckBox).Checked;
            Guid WeightTypeID, DropZoneID;
            if (Session["DropZoneID"] != null)
            {
                DropZoneID = new Guid(Session["DropZoneID"].ToString());
                RadComboBox cboWeightType = (RadComboBox)item.FindControl("cboWeightType");
                WeightTypeID = new Guid(cboWeightType.SelectedItem.Value);
            }
            else
            {
                WeightTypeID = new Guid(Session["WeightTypeID"].ToString());
                RadComboBox cboDropZone = (RadComboBox)item.FindControl("cboDropZone");
                DropZoneID = new Guid(cboDropZone.SelectedItem.Value);
            }

            Guid PricesID = Guid.NewGuid();
            t_Prices newPrices = new t_Prices();
            newPrices.ID = PricesID;
            newPrices.Prices = Prices;
            newPrices.WeightTypeID = WeightTypeID;
            newPrices.DropZoneID = DropZoneID;
            newPrices.Active = Active;
            newPrices.CreatedTime = DateTime.Now;
            db.t_Prices.Add(newPrices);
            db.SaveChanges();
            LoadGrid();
        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            //Update Item
            GridEditableItem item = e.Item as GridEditableItem;
            Guid PricesID = new Guid((item["NewID"].Controls[0] as TextBox).Text);

            String Prices = (item["Prices"].Controls[0] as TextBox).Text;
            Boolean Active = (item["Active"].Controls[0] as CheckBox).Checked;
            Guid WeightTypeID, DropZoneID;

            RadComboBox cboDropZone = (RadComboBox)item.FindControl("cboDropZone");
            RadComboBox cboWeightType = (RadComboBox)item.FindControl("cboWeightType");
            WeightTypeID = new Guid(cboWeightType.SelectedItem.Value);
            DropZoneID = new Guid(cboDropZone.SelectedItem.Value);

            db = new TARSDelivery_EprojectEntities();
            t_Prices newPrices = db.t_Prices.First(x => x.ID == PricesID);
            newPrices.Prices = Prices;
            newPrices.WeightTypeID = WeightTypeID;
            newPrices.DropZoneID = DropZoneID;
            newPrices.Active = Active;
            db.SaveChanges();
            LoadGrid();
        }
    }
}